package com.glory.gloryquickui.widget.lock_view.locktableview;

import android.content.Context;
import android.graphics.Paint;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;


import com.glory.gloryquickui.R;
import com.glory.gloryquickui.entity.TableCustomBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明
 * 作者 郭翰林
 * 创建时间 2017/9/17.
 */

public class UnLockColumnAdapter extends RecyclerView.Adapter<UnLockColumnAdapter.UnLockViewHolder> {
    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 表格数据
     */
    private ArrayList<ArrayList<TableCustomBean>> mTableDatas;
    /**
     * 第一行背景颜色
     */
    private int mFristRowBackGroudColor;
    /**
     * 表格头部字体颜色
     */
    private int mTableHeadTextColor;
    /**
     * 表格内容字体颜色
     */
    private int mTableContentTextColor;
    /**
     * 记录每列最大宽度
     */
    private ArrayList<Integer> mColumnMaxWidths = new ArrayList<Integer>();
    /**
     * 记录每行最大高度
     */
    private ArrayList<Integer> mRowMaxHeights = new ArrayList<Integer>();
    /**
     * 单元格字体大小
     */
    private int mTextViewSize;
    /**
     * 是否锁定首行
     */
    private boolean isLockFristRow = true;
    /**
     * 是否改变最后一列颜色
     */
    private boolean isLockLastRow = false;
    /**
     * 是否锁定首列
     */
    private boolean isLockFristColumn = true;

    /**
     * 单元格内边距
     */
    private int mCellPadding;

    /**
     * Item点击事件
     */
    private LockTableView.OnItemClickListenter mOnItemClickListenter;

    /**
     * 头部点击事件
     */
    private LockTableView.OnItemClickListenter mOnHeadItemClickListenter;


    /**
     * 最后一个ITEM点击时间
     */
    private LockTableView.OnLastItemClickListener mOnLastItemClickListener;

    /**
     * Item长按事件
     */
    private LockTableView.OnItemLongClickListenter mOnItemLongClickListenter;

    /**
     * Item项被选中监听(处理被选中的效果)
     */
    private TableViewAdapter.OnItemSelectedListenter mOnItemSelectedListenter;

    /**
     *  显示边框
     */
    private Boolean isShowBorder;

    /**
     * 显示条纹
     */
    private Boolean isStripe;

    /**
     * 构造方法
     *
     * @param mContext
     * @param mTableDatas
     */
    public UnLockColumnAdapter(Context mContext, ArrayList<ArrayList<TableCustomBean>> mTableDatas) {
        this.mContext = mContext;
        this.mTableDatas = mTableDatas;
    }


    @Override
    public int getItemCount() {
        return mTableDatas.size();
    }

    @Override
    public UnLockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        UnLockViewHolder holder = new UnLockViewHolder(LayoutInflater.from(mContext).inflate(R.layout.lock_unlock_item, null));
        return holder;
    }

    @Override
    public void onBindViewHolder(UnLockViewHolder holder, final int position) {
        ArrayList<TableCustomBean> datas = mTableDatas.get(position);
        if (isLockFristRow) {
            //第一行是锁定的
            createRowView(holder.mLinearLayout, datas, false, mRowMaxHeights.get(position + 1),position);
        } else {
            if (position == 0) {
                holder.mLinearLayout.setBackgroundColor(ContextCompat.getColor(mContext, mFristRowBackGroudColor));
                createRowView(holder.mLinearLayout, datas, true, mRowMaxHeights.get(position),position);
            } else {
                if(isStripe) {
                    if (position % 2 == 0) {
                        holder.mLinearLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.bg_gray));
                    } else {
                        holder.mLinearLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
                    }
                }else{
                    holder.mLinearLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
                }
                createRowView(holder.mLinearLayout, datas, false, mRowMaxHeights.get(position),position);
            }
        }
        //添加事件
        if(mOnItemClickListenter!=null){
            holder.mLinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mOnItemSelectedListenter!=null){
                        mOnItemSelectedListenter.onItemSelected(v,position);
                    }
                    if(isLockFristRow){
                        mOnItemClickListenter.onItemClick(v,position+1);
                    }else{
                        if(position!=0) {
                            mOnItemClickListenter.onItemClick(v, position);
                        }
                    }
                }
            });
        }
//        if(mOnHeadItemClickListenter != null){
//            holder.mLinearLayout.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if(position==0){
//                        mOnHeadItemClickListenter.onItemClick(v,position);
//                    }
//                }
//            });
//        }
        if(mOnItemLongClickListenter!=null){
            holder.mLinearLayout.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if(mOnItemSelectedListenter!=null){
                        mOnItemSelectedListenter.onItemSelected(v,position);
                    }
                    if (isLockFristRow){
                        mOnItemLongClickListenter.onItemLongClick(v,position+1);
                    }else{
                        if(position!=0){
                            mOnItemLongClickListenter.onItemLongClick(v,position);
                        }
                    }
                    return true;
                }
            });
        }
        //如果没有设置点击事件和长按事件
        if(mOnItemClickListenter==null&&mOnItemLongClickListenter==null&&mOnHeadItemClickListenter==null){
            holder.mLinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mOnItemSelectedListenter!=null){
                        mOnItemSelectedListenter.onItemSelected(v,position);
                    }
                }
            });
            holder.mLinearLayout.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if(mOnItemSelectedListenter!=null){
                        mOnItemSelectedListenter.onItemSelected(v,position);
                    }
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }


    //取得每行每列应用高宽
    public void setColumnMaxWidths(ArrayList<Integer> mColumnMaxWidths) {
        this.mColumnMaxWidths = mColumnMaxWidths;
    }

    public void setRowMaxHeights(ArrayList<Integer> mRowMaxHeights) {
        this.mRowMaxHeights = mRowMaxHeights;
    }

    public void setTextViewSize(int mTextViewSize) {
        this.mTextViewSize = mTextViewSize;
    }

    public void setLockFristRow(boolean lockFristRow) {
        isLockFristRow = lockFristRow;
    }

    public void setLockLastRow(boolean lockLastRow){
        isLockLastRow = lockLastRow;
    }

    public void setBorder(boolean isShowBorder){
        this.isShowBorder = isShowBorder;
    }

    public void setStripe(boolean isStripe){
        this.isStripe = isStripe;
    }

    public void setFristRowBackGroudColor(int mFristRowBackGroudColor) {
        this.mFristRowBackGroudColor = mFristRowBackGroudColor;
    }

    public void setTableHeadTextColor(int mTableHeadTextColor) {
        this.mTableHeadTextColor = mTableHeadTextColor;
    }

    public void setTableContentTextColor(int mTableContentTextColor) {
        this.mTableContentTextColor = mTableContentTextColor;
    }

    public void setLockFristColumn(boolean lockFristColumn) {
        isLockFristColumn = lockFristColumn;
    }

    public void setOnItemClickListenter(LockTableView.OnItemClickListenter mOnItemClickListenter) {
        this.mOnItemClickListenter = mOnItemClickListenter;
    }

    public void setOnHeadItemClickListenter(LockTableView.OnItemClickListenter mOnHeadItemClickListenter) {
        this.mOnHeadItemClickListenter = mOnHeadItemClickListenter;
    }

    public void setOnLastItemClickListener(LockTableView.OnLastItemClickListener mOnLastItemClickListenter) {
        this.mOnLastItemClickListener = mOnLastItemClickListenter;
    }

    public void setOnItemLongClickListenter(LockTableView.OnItemLongClickListenter mOnItemLongClickListenter) {
        this.mOnItemLongClickListenter = mOnItemLongClickListenter;
    }

    public void setOnItemSelectedListenter(TableViewAdapter.OnItemSelectedListenter mOnItemSelectedListenter) {
        this.mOnItemSelectedListenter = mOnItemSelectedListenter;
    }

    public void setCellPadding(int mCellPadding) {
        this.mCellPadding = mCellPadding;
    }

    class UnLockViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mLinearLayout;

        public UnLockViewHolder(View itemView) {
            super(itemView);
            mLinearLayout = (LinearLayout) itemView.findViewById(R.id.unlock_linearlayout);
        }
    }

    /**
     * 构造每行数据视图
     *
     * @param linearLayout
     * @param datas
     * @param isFristRow   是否是第一行
     */
    private void createRowView(LinearLayout linearLayout, List<TableCustomBean> datas, boolean isFristRow, int mMaxHeight, final int rowPos) {
        //设置LinearLayout
        linearLayout.removeAllViews();//首先清空LinearLayout,复用会造成重复绘制，使内容超出预期长度
        for (int i = 0; i < datas.size(); i++) {
            //构造单元格
            LinearLayout contentLayout = new LinearLayout(mContext);
            contentLayout.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams contentViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            contentViewParams.setMargins(mCellPadding, mCellPadding, mCellPadding, mCellPadding);
            contentViewParams.height = DisplayUtil.dip2px(mContext, mMaxHeight);
            if (isLockFristColumn) {
                contentViewParams.width = DisplayUtil.dip2px(mContext, mColumnMaxWidths.get(i+1));
            } else {
                contentViewParams.width = DisplayUtil.dip2px(mContext, mColumnMaxWidths.get(i));
            }
            contentLayout.setLayoutParams(contentViewParams);
            TextView textView = new TextView(mContext);
            if (isFristRow) {
                textView.setTextColor(ContextCompat.getColor(mContext, mTableHeadTextColor));
            } else {
                textView.setTextColor(ContextCompat.getColor(mContext, mTableContentTextColor));
                if (datas.size() - 1 == i){
                    if (isLockLastRow){
                        textView.setTextColor(mContext.getResources().getColor(R.color.lock_background));
                        textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
                        textView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (mOnLastItemClickListener != null) {
                                    mOnLastItemClickListener.onItemLastClick(v, rowPos);
                                }

                            }
                        });
                    }else {
                        textView.setTextColor(ContextCompat.getColor(mContext, mTableContentTextColor));
                    }
                }

            }
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextViewSize);
            textView.setGravity(Gravity.CENTER);
            textView.setText(datas.get(i).getTextName());
            //设置布局

            LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(0,
                    LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
//            textViewParams.setMargins(mCellPadding, mCellPadding, mCellPadding, mCellPadding);
//            textViewParams.height = DisplayUtil.dip2px(mContext, mMaxHeight);
//            if (isLockFristColumn) {
//                textViewParams.width = DisplayUtil.dip2px(mContext, mColumnMaxWidths.get(i+1));
//            } else {
//                textViewParams.width = DisplayUtil.dip2px(mContext, mColumnMaxWidths.get(i));
//            }
            textView.setLayoutParams(textViewParams);
            contentLayout.addView(textView);

            ImageView imageView = new ImageView(mContext) ;
            LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            imageViewParams.gravity = Gravity.CENTER;
            imageView.setLayoutParams(imageViewParams);
            if(datas.get(i).getSort() == ""){
                imageView.setImageResource(R.drawable.icon_sort_normal);
            }else if (datas.get(i).getSort() == "asc"){
                imageView.setImageResource(R.drawable.icon_sort_asc);
            }else if (datas.get(i).getSort() == "desc"){
                imageView.setImageResource(R.drawable.icon_sort_desc);
            }
            contentLayout.addView(imageView);
            if (isFristRow) {
                if(datas.get(i).isHeadSort()) {
                    imageView.setVisibility(View.VISIBLE);
                    final int finalI = i;
                    contentLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnHeadItemClickListenter.onItemClick(v, finalI);
                        }
                    });
                }else{
                    imageView.setVisibility(View.INVISIBLE);
                }
            }else{
                imageView.setVisibility(View.INVISIBLE);
            }

            if (isShowBorder) {
                //画分隔线
                if (i != datas.size() - 1) {
                    View splitView = new View(mContext);
                    ViewGroup.LayoutParams splitViewParmas = new ViewGroup.LayoutParams(DisplayUtil.dip2px(mContext, 1),
                            ViewGroup.LayoutParams.MATCH_PARENT);
                    splitView.setLayoutParams(splitViewParmas);
                    if (isFristRow) {
                        splitView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
                    } else {
                        splitView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.light_gray));
                    }
                    contentLayout.addView(splitView);
                }
            }
            linearLayout.addView(contentLayout);
        }
    }
}
